package com.naizhuai.ui.components;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.annotation.StringDef;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.naizhuai.Model.AccountHelper;
import com.naizhuai.R;
import com.naizhuai.ui.dynamic.DynamicFragment;
import com.naizhuai.ui.home.HomeFragment;
import com.naizhuai.ui.task.ActionFragment;
import com.naizhuai.ui.task.ActionSetting1Activity;
import com.naizhuai.ui.user.userinfo.InfoFragment;
import com.naizhuai.ui.web.WebActivity;
import com.naizhuai.util.DialogUtils;

import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class NavTabView extends LinearLayout {

    public static final String HOME="home";
    public static final String ACTION="action";
    public static final String STUDY="study";
    public static final String ME="me";

    @BindView(R.id.nav_item_home)
    NavigationTab navItemHome;
    @BindView(R.id.nav_item_action)
    NavigationTab navItemAction;
    @BindView(R.id.fab_add_task)
    FloatingActionButton fabAddTask;
    @BindView(R.id.nav_item_study)
    NavigationTab navItemStudy;
    @BindView(R.id.nav_item_me)
    NavigationTab navItemMe;
    private Context mContext;
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private NavigationTab mCurrentNavTab;
    private Unbinder unBind;
    private SelectCallBack mSelectCallBack;

    public NavTabView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public NavTabView(Context context) {
        this(context, null);
    }

    public NavTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    public void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.nav_fragment, this,true);
        unBind= ButterKnife.bind(this);
        navItemHome.init(R.drawable.tab_icon_world, R.string.main_world, HomeFragment.class,HOME);
        navItemAction.init(R.drawable.tab_icon_action, R.string.main_action, ActionFragment.class,ACTION);
        navItemStudy.init(R.drawable.tab_icon_study, R.string.main_study, DynamicFragment.class,STUDY);
        navItemMe.init(R.drawable.tab_icon_me, R.string.main_personal, InfoFragment.class,ME);
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        unBind.unbind();
    }

    public void config(Context context, SelectCallBack callBack, FragmentManager fragmentManager,
                       @IdRes int mContainerId) {
        this.mContext = context;
        this.mSelectCallBack=callBack;
        this.mFragmentManager = fragmentManager;
        this.mContainerId = mContainerId;
        clearOldFragment();
        doselect(navItemHome);
    }
    @SuppressWarnings("RestrictedApi")
    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0)
            return;
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != null) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit)
            transaction.commitNow();
    }
    private void doselect(NavigationTab newNavTab) {
        NavigationTab oldNavTab = null;
        if (mCurrentNavTab != null) {
            oldNavTab = mCurrentNavTab;
            if (oldNavTab == newNavTab) {
                // TODO: 2018/4/26 you can do something
                return;
            }
            oldNavTab.setSelected(false);
        }
        newNavTab.setSelected(true);
        doTabChange(oldNavTab, newNavTab);
        mCurrentNavTab = newNavTab;
        mSelectCallBack.titleChange(mCurrentNavTab.getNavTitle(),mCurrentNavTab.getTag());

    }

    private void doTabChange(NavigationTab oldNavTab, NavigationTab newNavTab) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavTab != null) {
            if (oldNavTab.getFragment() != null) {
                ft.hide(oldNavTab.getFragment());
            }
        }
        if (newNavTab != null) {
            if (newNavTab.getFragment() == null) {
                Fragment fragment = Fragment.instantiate(mContext, newNavTab.getClz().getName(), null);
                ft.add(mContainerId, fragment, fragment.getTag());
                newNavTab.setFragment(fragment);
            } else {
                ft.show(newNavTab.getFragment());
            }
        }
        ft.commit();
    }

    @OnClick({R.id.nav_item_home, R.id.nav_item_action, R.id.fab_add_task, R.id.nav_item_study, R.id.nav_item_me})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.nav_item_home:
            case R.id.nav_item_action:
            case R.id.nav_item_study:
            case R.id.nav_item_me:
                doselect((NavigationTab)view);
                break;
            case R.id.fab_add_task:
                doHandinHand();
                break;
        }
    }

    /**
     * 随手传
     */
    private void doHandinHand() {
        if (TextUtils.isEmpty(AccountHelper.getComPanyid())){
            DialogUtils.showPromptDialog(getContext(),getContext().getString(R.string.please_addcompany));
        }else {
            ActionSetting1Activity.start(mContext);
        }

    }

    public interface SelectCallBack {
        void titleChange(String title,String tag);
    }

}
